package luanvan.app.foodnote.library;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.text.TextUtils;
import android.util.Log;


public class HttpRetriever {
	private static String Address1 = "";

	private static String Address2 = "";

	private static String City = "";

	private static String State = "";

	private static String Country = "";

	private static String County = "";

	private static String PIN = "";

	public static final String GOOGLE_GEOCODER = "http://maps.googleapis.com/maps/api/geocode/json?latlng=";

	private DefaultHttpClient client = new DefaultHttpClient();
	
 	public static String getAddress1()
    {
        return Address1;

    }

    public static String getAddress2()
    {
        return Address2;

    }

    public static String getCity()
    {
        return City;

    }

    public static String getState()
    {
        return State;

    }

    public static String getCountry()
    {
        return Country;

    }

    public static String getCounty()
    {
        return County;

    }

    public static String getPIN()
    {
        return PIN;

    }
	
	public String retrieve(String url){
		HttpGet getRequest = new HttpGet(url);
		try {
			HttpResponse getResponse = client.execute(getRequest);
			final int statusCode = getResponse.getStatusLine().getStatusCode();
			if(statusCode != HttpStatus.SC_OK){
				Log.w(getClass().getSimpleName(), "Error " + statusCode
						+ " for URL " + url);
				return null;
			}
			HttpEntity getResponseEntity = getResponse.getEntity();
			if(getResponseEntity != null){
				return EntityUtils.toString(getResponseEntity);
			}
		} catch (IOException e){
			getRequest.abort();
			Log.w(getClass().getSimpleName(), "Error for URL " + url, e);
		}
		return null;
	}
	
	public void getAddressFromGPSData(double lat, double lng){
		Address1 = "";  Address2 = ""; City = ""; State = ""; Country = ""; County = ""; PIN = "";
		HttpRetriever agent = new HttpRetriever();
		String request = GOOGLE_GEOCODER + lat + "," + lng + "&sensor=true";
		String response = agent.retrieve(request);
		//String formattedAddress = "";
		if(response != null){
			Log.d("GeoCoder", response);
			try {
				JSONObject parentObject = new JSONObject(response);
				JSONArray arrayOfAddressResults = parentObject.getJSONArray("results");
				JSONObject addressItem = arrayOfAddressResults.getJSONObject(0);
				JSONArray address_components = addressItem.getJSONArray("address_components");
				for(int i = 0; i < address_components.length(); i++){
					JSONObject zero = address_components.getJSONObject(i);
					String long_name = zero.getString("long_name");
					JSONArray mtypes = zero.getJSONArray("types");
					String Type = mtypes.getString(0);
					
					if(TextUtils.isEmpty(long_name) == false || 
							!long_name.equals(null) || long_name.length() > 0 || long_name != "" ){
						if(Type.equalsIgnoreCase("street_number")){
							Address1 = long_name + " ";
						} else if(Type.equalsIgnoreCase("route")){
							Address1 = Address1 + long_name;
						} else if(Type.equalsIgnoreCase("sublocality")){
							Address2 = long_name;
						} else if(Type.equalsIgnoreCase("locality")){
							City = long_name;
						} else if(Type.equalsIgnoreCase("administrative_area_level_2")){
							County = long_name;
						} else if(Type.equalsIgnoreCase("administrative_area_level_1")){
							State = long_name;
						} else if(Type.equalsIgnoreCase("country")){
							Country = long_name;
						} else if(Type.equalsIgnoreCase("postal_code")){
							PIN = long_name;
						}
					}
				}
			} catch (JSONException e){
				e.printStackTrace();
			}
		}

	}
	
}
